//
//  ZLPullMenuContentView.m
//  ZLPullMenu
//
//  Created by 赵振磊 on 2024/5/27.
//

#import "ZLPullMenuContentView.h"
#import "ZLPullMenuContentCell.h"
#import "ZLPullMenuContentReusableView.h"
#import <Masonry/Masonry.h>

@interface ZLPullMenuContentView()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
/// 列表
@property (nonatomic, strong) UICollectionView *collectionView;
/// 布局
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
/// 数据源
@property (nonatomic, copy) NSArray *groups;
@end

@implementation ZLPullMenuContentView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsZero);
        }];
    }
    return self;
}

- (CGFloat)getMenuHeight {
    CGFloat height = 0;
    for (ZLPullMenuGroup *group in self.groups) {
        NSInteger line = group.options.count / 4;
        CGFloat contentHeight = (line * 10 + (line + 1) * 25);
        height = height + 40 + contentHeight;
    }
    return height + 10;
}

- (void)reloadData:(NSArray<ZLPullMenuGroup *> *)groups {
    self.groups = groups;
    [self.collectionView reloadData];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.groups.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    ZLPullMenuGroup *group = self.groups[section];
    return group.options.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(self.frame.size.width, 40);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((self.frame.size.width - 50) / 4, 25);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (![kind isEqualToString:UICollectionElementKindSectionHeader]) {
        return nil;
    }
    ZLPullMenuContentReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ZLPullMenuContentReusableView" forIndexPath:indexPath];
    ZLPullMenuGroup *group = self.groups[indexPath.section];
    reusableView.group = group;
    return reusableView;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ZLPullMenuContentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZLPullMenuContentCell" forIndexPath:indexPath];
    ZLPullMenuGroup *group = self.groups[indexPath.section];
    ZLPullMenuOption *option = group.options[indexPath.row];
    cell.option = option;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.delegate respondsToSelector:@selector(pullMenuContentViewWillSelected:)]) {
        if (![self.delegate pullMenuContentViewWillSelected:indexPath]) {
            return;
        }
    }
    ZLPullMenuGroup *group = self.groups[indexPath.section];
    ZLPullMenuOption *selectOption = group.options[indexPath.row];
    NSInteger selectedCount = 0;
    for (ZLPullMenuOption *op in group.options) {
        if (op.selected) {
            selectedCount++;
        }
    }
    if (selectOption.selected && group.mustSelect && selectedCount == 1) {
        NSLog(@"必选不可取消");
        return;
    } else {
        selectOption.selected = !selectOption.selected;
    }
    if (!group.selecteds) {
        for (ZLPullMenuOption *option in group.options) {
            if (selectOption != option) {
                option.selected = NO;
            }
        }
    }
    [self.collectionView reloadData];
    if ([self.delegate respondsToSelector:@selector(pullMenuContentViewDidSelected:)]) {
        [self.delegate pullMenuContentViewDidSelected:indexPath];
    }
}

#pragma mark - init

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[ZLPullMenuContentCell class] forCellWithReuseIdentifier:@"ZLPullMenuContentCell"];
        [_collectionView registerClass:[ZLPullMenuContentReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ZLPullMenuContentReusableView"];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.contentInset = UIEdgeInsetsMake(0, 10, 10, 10);
    }
    return _collectionView;
}

- (UICollectionViewFlowLayout *)layout {
    if (!_layout) {
        _layout = [[UICollectionViewFlowLayout alloc] init];
        _layout.minimumLineSpacing = 10;
        _layout.minimumInteritemSpacing = 10;
        _layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    }
    return _layout;
}

@end
